package br.com.strings_serializacao;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * Objetivo: 
 * @author Sidney
 *
 */
class Celular implements Serializable{
	String marca = "LG";
	transient int numSerie = 123;
	public String toString(){
		return "Marca: " + marca + " Numero Serie: " + numSerie;
	}
	private void writeObject(ObjectOutputStream os){
		try{
			os.defaultWriteObject();
			os.write(1200);
		}catch(IOException e){System.out.println(e);}
	}
	private void readObject(ObjectInputStream is){
		try{
			is.defaultReadObject();
			numSerie = is.readInt();	
		}catch(Exception e){e.printStackTrace();}
}
}
public class ExemploSerializacao6 {
	public static void main(String[] args) {
		Celular cel = new Celular();
		System.out.println(cel);
		try{
			FileOutputStream fs = new FileOutputStream("c://temp/testecel.txt");
			ObjectOutputStream os = new ObjectOutputStream(fs);
			os.writeObject(cel);//serializa
			os.flush();
			os.close();
			
			FileInputStream fi = new FileInputStream("c://temp/testecel.txt");
			ObjectInputStream oi = new ObjectInputStream(fi);
			cel = (Celular) oi.readObject();
			System.out.println(cel);
			//System.out.println(oi.readObject());
		}catch(Exception f){f.printStackTrace();}	
	}
}


